package TwoAgain;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    private LovoTable table = new LovoTable(this,new String[]{"编号","名字","单价"},new String[]{"code","name","price"},
            "code");
    private ICommodityService service = new CommodityServiceImpl();

    public MainFrame(){
        this.setLayout(null);
        this.init();

        this.setSize(500,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        table.updateLovoTable(service.showAll());

        table.setSizeAndLocation(20,80,440,350);
        LovoButton buyButton = new LovoButton("购买",350,480,this);
        LovoLabel infoLabel = new LovoLabel("欢迎选购商品",50,40,this);

        buyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buy();
            }
        });
    }

    private void buy(){
        String x = table.getKeyByString();
        if (x==null){
            JOptionPane.showMessageDialog(null,"请选中行");
        }else {
            int code = Integer.parseInt(x);
//            System.out.println(code);
            Commodity c = service.search(code);
            this.dispose();
            new ShopFrame(c);

        }

    }



    public static void main(String[] args) {
        new MainFrame();
    }
}
